package com.shxc.smarttrade.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.shxc.smarttrade.R;

import java.util.Calendar;


/**
 * 日期弹窗
 */
public class DateDialog extends Dialog {

    public View view;
    private Context c;
    private DatePicker datepicker;
    private TextView submit_textview;
    private TextView cancel_texxtview;

    private int thisyear, thismonth, thisday;
    private boolean isCheck = false;


    public DateDialog(Context paramContext, int paramInt) {
        super(paramContext, paramInt);
        view = View.inflate(paramContext, R.layout.dialog_date, null);
        this.setContentView(view);
        this.c = paramContext;
        datepicker = findViewById(R.id.datepicker);
        submit_textview = findViewById(R.id.submit_textview);
        cancel_texxtview = findViewById(R.id.cancel_texxtview);
        //不允许改数字
        datepicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
        //不允许显示日历
        datepicker.setCalendarViewShown(false);
//        CalendarView和Spinners的值分别为false和true
        datepicker.setCalendarViewShown(false);
        datepicker.setSpinnersShown(true);
    }

    /**
     * 显示生日弹出框
     */
    public void showDialog(int year, int month, int day, final boolean isCheck) {
        // 获取当前的年、月、日
        Calendar c = Calendar.getInstance();
        thisyear = c.get(Calendar.YEAR);
        thismonth = c.get(Calendar.MONTH);
        thisday = c.get(Calendar.DAY_OF_MONTH);
        this.isCheck = isCheck;
        if (year == 0) {
            year = thisyear;
            month = thismonth;
            day = thisday;
        }

        datepicker.init(year, month-1, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar c = Calendar.getInstance();
                if (isCheck) {
                    if (isDateBefore(view)) {
                        view.init(thisyear, thismonth, thisday, this);
                    }
                }else{
                    if (isDateAfter(view)) {
                        view.init(thisyear, thismonth, thisday, this);
                    }
                }

            }

            private boolean isDateBefore(DatePicker tempView) {
                if (tempView.getYear() < thisyear) {
                    return true;
                } else if (tempView.getYear() == thisyear && tempView.getMonth() < thismonth) {
                    return true;
                } else if (tempView.getYear() == thisyear && tempView.getMonth() == thismonth && tempView.getDayOfMonth() < thisday) {
                    return true;
                } else {
                    return false;
                }
            }
            private boolean isDateAfter(DatePicker tempView) {
                if (tempView.getYear() > thisyear) {
                    return true;
                } else if (tempView.getYear() == thisyear && tempView.getMonth() > thismonth) {
                    return true;
                } else if (tempView.getYear() == thisyear && tempView.getMonth() == thismonth && tempView.getDayOfMonth() > thisday) {
                    return true;
                } else {
                    return false;
                }
            }
        });


        this.show();
    }

    public void cancel() {
        if (!isShowing())
            return;
        super.cancel();
    }

    public void show() {
        if (isShowing())
            return;
        super.show();
    }

    public DatePicker getDatepicker() {
        return datepicker;
    }

    public TextView getSubmit_textview() {
        return submit_textview;
    }

    public TextView getCancel_texxtview() {
        return cancel_texxtview;
    }
}
